////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.foreignexchange;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.ForeignExchangeGroup;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnHolder;
import cc.telecomdigital.tdstock.adapter.TwoTextColumnItemAdapter;

/**
 * @author haibo for list item named "Currency Exchange"
 */
public class CurrencyExchangeActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String		TAG			= CurrencyExchangeActivity.class.getSimpleName();
	private static final long			START_ID	= 90000l;
	private TextView					_topBar;
	private ListView					_listView;
	private Button						_toolBtnReturn;
	private TwoTextColumnItemAdapter	_adapter;
	private List<TwoTextColumnHolder>	_list;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.currency_exchange);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

//				Intent intent = new Intent(getApplicationContext(), ForeignExchangeRateOptionsListActivity.class);
//				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
//				SwitchForwardActivity(ForeignExchangeRateOptionsListActivity.class, intent);
				
				SwitchBackActivity();
			}
		});

		_topBar = (TextView) findViewById(R.id.top_bar_title);
		_listView = (ListView) findViewById(R.id.exchange_rate_list);

		_list = new ArrayList<TwoTextColumnHolder>();

		_adapter = new TwoTextColumnItemAdapter(LayoutInflater.from(this), _list);

		_listView.setAdapter(_adapter);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		updateTitle();
	}

	private void updateAdapter(List<String[]> list)
	{
		_adapter.notifyDataSetChanged();
		if (list == null || list.isEmpty())
		{
			_adapter.notifyDataSetChanged();
			return;
		}
		
		/*
		 防止listview添加多次 changed by Max 2012-01-11
		 */
		if(_list!=null&&!_list.isEmpty())
			_list.clear();

		// hard code sort index as the same to the iphone side;
		String[] fxName = { "USD", "GBP", "CHF", "CAD", "EUR", "AUD", "NZD",
				"JPY", "CNY", "KRW", "TWD", "SGD", "THB" };
		HashMap<String, String> fxNameToValue = new HashMap<String, String>();
		for (String key : fxName)
		{
			fxNameToValue.put(key, "---,---");
		}

		int index = 0;
		StringBuffer buff = new StringBuffer();
		for (String[] array : list)
		{
			String key = array[3].trim();
			if (fxNameToValue.containsKey(key))
			{
				buff.setLength(0);
				buff.append(array[0]).append(",").append(
						array[1] + "/" + array[2]);
				fxNameToValue.put(key, buff.toString());
				index++;
			}
			if (index >= fxNameToValue.size())
				break;
		}

		index = 0;
		for (String fx : fxName)
		{
			String[] array = fxNameToValue.get(fx).split("[,]");
			TwoTextColumnHolder holder = new TwoTextColumnHolder();
			holder.left = new TextView(this);
			holder.right = new TextView(this);
			holder.id = START_ID + index++;
			holder.left.setText(array[0]);
			holder.right.setText(array[1]);
			_list.add(holder);
		}

		((ITDLApplication) getApplicationContext()).setObject(null);
		_adapter.notifyDataSetChanged();
	}

	private void updateTitle()
	{
		String title = ((ITDLApplication) getApplicationContext())
				.getString(CommonDefn.TOP_BAR_TITLE);
		if (_topBar != null && title != null)
			_topBar.setText(title);
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return ForeignExchangeGroup.GetActivityGroupHostControl();
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		return HttpAppInfo.SendFXCurrencyRequest(actContext, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if(!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError+"("+iResult.MStatus+")");
			return;
		}
		
		Runnable runnable = new Runnable()
		{
			public void run()
			{
				updateAdapter(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}

}
